package com.avalon.erp.addon.pet.service.user.pet;

import com.avalon.core.exception.AvalonException;
import com.avalon.core.field.Field;
import com.avalon.core.field.Fields;
import com.avalon.core.model.PrimaryKey;
import com.avalon.core.model.RecordRow;
import com.avalon.core.service.AbstractService;
import com.avalon.erp.addon.pet.model.enums.CheckStateEnum;
import com.avalon.erp.addon.pet.model.enums.PetAdoptStateEnum;
import com.avalon.erp.addon.pet.service.user.PetUserPetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class PetAdoptService extends AbstractService {
    private PetUserPetService petUserPetService;

    public PetAdoptService(PetUserPetService petUserPetService) {
        this.petUserPetService = petUserPetService;
    }

    @Override
    public String getServiceName() {
        return "pet.adopt";
    }

    @Override
    public String getLabel() {
        return "宠物转养,领养";
    }

    public final Field petId = Fields.createMany2one("宠物", "pet.user.pet");
    public final Field selfDescription = Fields.createString("自述");
    public final Field publishId = Fields.createMany2one("发布人", "pet.user");

    public final Field avatar = Fields.createImage("首页");
    public final Field video = Fields.createImage("视频");

    public final Field state = Fields.createSelection("状态", PetAdoptStateEnum.class);
    public final Field checkState = Fields.createSelection("审核状态", CheckStateEnum.class, CheckStateEnum.checking);

    @Override
    public PrimaryKey insert(RecordRow recordRow) throws AvalonException {
        if (!recordRow.containsKey("state")) {
            recordRow.put("state", PetAdoptStateEnum.adopting);
        }

        if (!recordRow.containsKey(avatar)) {
            if (!recordRow.containsKey(checkState)) {
                recordRow.put(checkState, CheckStateEnum.success);
            }
        }

        PrimaryKey key = super.insert(recordRow);
        petUserPetService.petSending(recordRow.getInteger(petId));
        return key;
    }

    @Override
    public Integer delete(RecordRow row) throws AvalonException {
        petUserPetService.petNormal(row.getInteger(petId));
        return super.delete(row);
    }
}
